# 这是一个用python来向thingspeak发post请求的例子，这样在thingspeak平台上就能看到曲线了

from urllib import *
import urllib2
# import httplib
import random
import time


#urlretrieve('http://www.baidu.com','d:\\test.html')
#raw_input("stop:")


getfeedurl = "http://api.thingspeak.com/channels/16960/feed.json"
updateurl = "http://api.thingspeak.com/update"

while 1:
	intdata = random.randint(1,100)
	print "random data:" + str(intdata)

	para = urllib.urlencode({'api_key':'9T9GQVEVL9TVALII','field1':intdata})
	resp = urllib2.urlopen(updateurl,para)
	ret = resp.read()
	print "return:" + ret
	print
	time.sleep(14) 
	# must not smaller than 13,or POST probably fail


'''
url = "http://api.thingspeak.com/channels/16960/feed.json"
conn = httplib.HTTPConnection('api.thingspeak.com')
conn.request(method='GET',url=url)
resp = conn.getresponse()
htmldata = resp.read()
print htmldata
'''

print
raw_input("enter:")
sys.exit()